package com.skirmisher.inspection.rectify.controller;

import com.skirmisher.inspection.common.utils.PageUtils;
import com.skirmisher.inspection.inspection.testController.ResponseModel;
import com.skirmisher.inspection.rectify.entity.InsJobRectify;
import com.skirmisher.inspection.rectify.entity.InsJobRectifyDetail;
import com.skirmisher.inspection.rectify.entity.InsJobRectifyPageQuery;
import com.skirmisher.inspection.rectify.entity.InsJobRectifySave;
import com.skirmisher.inspection.rectify.service.InsJobRectifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName InsJobRectifyAppController
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/14 下午9:22
 * @Version 1.0
 */
@Api(tags = "巡查整改app端接口")
@RestController
@RequestMapping("/app_jobRectify")
public class InsJobRectifyAppController {

    @Autowired
    private InsJobRectifyService insJobRectifyService;

    /**
     * @Author hanwangxin
     * @Description //TODO  * @Date 下午8:43 2020/12/15
     * @Param [workCode, jobType]
     * @return com.skirmisher.inspection.inspection.testController.ResponseModel
     **/
    @ApiOperation(value = "app端获取当前执行人的整改任务")
    @PostMapping(value = "/getInsRectify")
    public ResponseModel getInsRectify(InsJobRectifyPageQuery query){
        ResponseModel responseModel = new ResponseModel();

        //参数校验
        Integer startRow = PageUtils.getStartRow(query.getPage(), query.getRows());
        if (startRow == null || startRow < 0){
            responseModel.setCode(201);
            responseModel.setMessage("分页参数有误");
            return  responseModel;
        }
        query.setStartRows(startRow);
        insJobRectifyService.rectifyPageQuery(query);
        responseModel.setDataDataGrid(query.getDateList(), query.getCount());

        return  responseModel;
    }

    @ApiOperation(value = "整改任务详情数据")
    @GetMapping(value = "/rectifyDetail")
    @ApiImplicitParam(name = "rectifyId",value = "整改任务id",required = true)
    public ResponseModel rectifyDetail(String rectifyId){

        ResponseModel responseModel = new ResponseModel();
        if (StringUtils.isEmpty(rectifyId)){
            responseModel.setCode(201);
            responseModel.setMessage("无任务id");
            return responseModel;
        }
        InsJobRectify rectify = insJobRectifyService.rectifyDetail(rectifyId);
        responseModel.setData(rectify);
        return  responseModel;
    }

    @ApiOperation(value = "提交巡查任务整改")
    @PostMapping(value = "/saveInsRectify")
    public ResponseModel saveInsRectify(@RequestBody @Validated InsJobRectifySave save){

        ResponseModel responseModel = new ResponseModel();

        String rectifyId = insJobRectifyService.saveInsRectify(save);
        responseModel.setData(rectifyId);
        return  responseModel;
    }

    @ApiOperation(value = "整改任务整改明细提交")
    @PostMapping(value = "/submitRectify")
    public ResponseModel submitRectify(@RequestBody @Validated InsJobRectifyDetail detail){
        ResponseModel responseModel = new ResponseModel();

        insJobRectifyService.submitRectify(detail);

        return  responseModel;
    }

    @ApiOperation(value = "整改任务完成")
    @PostMapping(value = "/finishRectify")
    public ResponseModel finishRectify(@RequestBody String recId){
        ResponseModel responseModel = new ResponseModel();

        insJobRectifyService.finishRectify(recId);

        return  responseModel;
    }
}
